package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.BasicDao;
import com.qfedu.mtl.dto.BasicDto;
import com.qfedu.mtl.entity.BasicInfo;
import com.qfedu.mtl.entity.InfoDetail;
import com.qfedu.mtl.query.BasicQuery;
import com.qfedu.mtl.query.SelectQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanHandler;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.MapHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BasicDaoImpl implements BasicDao {
    @Override
    public int add(BasicDto dto) {
        String sql = "insert into tb_basic_info(basic_info_name,basic_info_status)values(?,?)";
        int i = DBUtil.executeDML(sql, DBUtil.getConn(), dto.getBasic_info_name(), dto.getBasic_info_status());
        return i;
    }

    @Override
    public int delete(int id) {
        String sql = "delete from tb_basic_info where basic_info_id=?";
        int i = DBUtil.executeDML(sql, DBUtil.getConn(), id);
        return i;
    }
    //批量删除  类目
    @Override
    public int deleteBatch(List<Integer> ids) {
        String sql = "delete from tb_basic_info where basic_info_id in (";
        for (int i = 0; i < ids.size(); i++) {
            if( i!= ids.size()-1){
                sql += " ?, ";
            }else{
                sql += "?)";
            }
        }
        return DBUtil.executeDML(sql,DBUtil.getConn(),ids.toArray());
    }

    @Override
    public BasicInfo select(Integer id) {
        String sql = "select * from tb_basic_info where basic_info_id=?";
        BasicInfo basicInfo = DBUtil.executeDQL(sql, DBUtil.getConn(), new BeanHandler<>(BasicInfo.class, true), id);
        return basicInfo;
    }

    @Override
    public int update(BasicDto dto) {
        String sql = "update tb_basic_info set basic_info_name=?,basic_info_status=? where basic_info_id=?";
        int i = DBUtil.executeDML(sql, DBUtil.getConn(), dto.getBasic_info_name(), dto.getBasic_info_status(), dto.getBasic_info_id());
        return i;
    }


    //根据类目的id找对应选项
    @Override
    public List<InfoDetail> findDetail(Integer id) {
        String sql = "select * from tb_info_detail where fk_basic_info_id=?";
        List<InfoDetail> infoDetails = DBUtil.executeDQL(sql, DBUtil.getConn(), new BeanListHandler<>(InfoDetail.class, true), id);
        return infoDetails;
    }

    //查询所有类目
    @Override
    public List<BasicInfo> selectAll() {
        String sql = "select * from tb_basic_info";
        List<BasicInfo> basicInfos = DBUtil.executeDQL(sql, DBUtil.getConn(), new BeanListHandler<>(BasicInfo.class, true));
        return basicInfos;
    }

    @Override
    public int addDetail(InfoDetail obj) {
        String sql = "insert into tb_info_detail(info_detail_name,info_detail_desc,fk_basic_info_id)values(?,?,?)";
        int i = DBUtil.executeDML(sql, DBUtil.getConn(), obj.getInfoDetailName(), obj.getInfoDetailDesc(), obj.getFkBasicInfoId());
        return i;
    }

    //删除单条记录
    @Override
    public int deleteDetail(Integer id) {
        String sql = "delete from tb_info_detail where info_detail_id=?";
        int i = DBUtil.executeDML(sql, DBUtil.getConn(), id);
        return i;
    }

    //批量删除
    @Override
    public int deleteBatch2(List<Integer> ids) {
        String sql = "delete from tb_info_detail where info_detail_id in (";
        for (int i = 0; i < ids.size(); i++) {
            if( i!= ids.size()-1){
                sql += " ?, ";
            }else{
                sql += "?)";
            }
        }
        return DBUtil.executeDML(sql,DBUtil.getConn(),ids.toArray());
    }

    @Override
    public int updateDetail(InfoDetail obj) {
        String sql = "update tb_info_detail set info_detail_name=?,info_detail_desc=?,fk_basic_info_id=? where info_detail_id=?";
        int i = DBUtil.executeDML(sql, DBUtil.getConn(), obj.getInfoDetailName(), obj.getInfoDetailDesc(), obj.getFkBasicInfoId(),obj.getInfoDetailId());
        return i;
    }

    //得到单个选项的价格，返回给业务层来计算总的价格
    @Override
    public int getPrice(int g_id, int id) {
        String sql = "select good_discount from tb_good_detail where fk_good_id=? and fk_info_detail_id=?";
//        Long aLong = DBUtil.executeDQL(sql, DBUtil.getConn(), new NumberHandler<>(Long.class),g_id,i);
//        return aLong.intValue();
        Map<String, Object> map = DBUtil.executeDQL(sql, DBUtil.getConn(), new MapHandler(), g_id,id);
        Object s = map.get("good_discount");
        System.out.println("保值：" + s);
        return Integer.parseInt(s.toString());

    }

    //根据商品的id查参考价格
    @Override
    public int findBasicPrice(int g_id) {
        String sql = "select good_cost from tb_good where good_id=?";
        Map<String, Object> map = DBUtil.executeDQL(sql, DBUtil.getConn(), new MapHandler(), g_id);
        Object s = map.get("good_cost");
        System.out.println("基本价格：" + s);
        return Integer.parseInt(s.toString());
    }

    //以下四个是类目和选项的分页查询
    @Override
    public List<BasicInfo> selectData(Integer page, Integer size, BasicQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select * from tb_basic_info where 1=1 ");
        if(!StrUtil.isNullOrEmpty( query.getBasic_info_name() )){
            sqlBuilder.append(" and  basic_info_name like concat('%',?,'%') ");
            params.add( query.getBasic_info_name()  );
        }
        if( query.getBasic_info_status()!=null ){
            sqlBuilder.append(" and  basic_info_status like concat('%',?,'%') ");
            params.add( query.getBasic_info_status() );
        }
        sqlBuilder.append(" limit ?,?");
        params.add(  (page-1)*size  );
        params.add( size );
        return DBUtil.executeDQL(sqlBuilder.toString(), DBUtil.getConn(),new BeanListHandler<>(BasicInfo.class,true), params.toArray() );
    }
    @Override
    public Integer selectTotal(BasicQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select count(*) from tb_basic_info where 1=1 ");
        if(!StrUtil.isNullOrEmpty( query.getBasic_info_name() )){
            sqlBuilder.append(" and  basic_info_name like concat('%',?,'%')");
            params.add( query.getBasic_info_name()  );
        }
        if( query.getBasic_info_status()!=null ){
            sqlBuilder.append(" and  basic_info_status like concat('%',?,'%') ");
            params.add( query.getBasic_info_status() );
        }
        Long aLong = DBUtil.executeDQL(sqlBuilder.toString(), new NumberHandler<>(Long.class), params.toArray());
        return aLong.intValue();

    }
    @Override
    public List<InfoDetail> selectData2(Integer page, Integer size, SelectQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select * from tb_info_detail where 1=1 ");
        if(!StrUtil.isNullOrEmpty( query.getInfoDetailName() )){
            sqlBuilder.append(" and  info_detail_name like concat('%',?,'%') ");
            params.add( query.getInfoDetailName()  );
        }
        if( query.getInfoDetailDesc()!=null ){
            sqlBuilder.append(" and  info_detail_desc like concat('%',?,'%') ");
            params.add( query.getInfoDetailDesc() );
        }
        if( query.getFkBasicInfoId()!=null ){
            sqlBuilder.append(" and  fk_basic_info_id like concat('%',?,'%') ");
            params.add( query.getFkBasicInfoId() );
        }
        sqlBuilder.append(" limit ?,?");
        params.add(  (page-1)*size  );
        params.add( size );
        return DBUtil.executeDQL(sqlBuilder.toString(), DBUtil.getConn(),new BeanListHandler<>(InfoDetail.class,true), params.toArray() );
    }
    @Override
    public Integer selectTotal2(SelectQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select count(*) from tb_info_detail where 1=1 ");
        if(!StrUtil.isNullOrEmpty( query.getInfoDetailName() )){
            sqlBuilder.append(" and  info_detail_name like concat('%',?,'%') ");
            params.add( query.getInfoDetailName()  );
        }
        if( query.getInfoDetailDesc()!=null ){
            sqlBuilder.append(" and  info_detail_desc like concat('%',?,'%') ");
            params.add( query.getInfoDetailDesc() );
        }
        if( query.getFkBasicInfoId()!=null ){
            sqlBuilder.append(" and  fk_basic_info_id like concat('%',?,'%') ");
            params.add( query.getFkBasicInfoId() );
        }
        Long aLong = DBUtil.executeDQL(sqlBuilder.toString(), new NumberHandler<>(Long.class), params.toArray());
        return aLong.intValue();
    }

    //以下两个是根据类目id的分页查询
    @Override
    public List<InfoDetail> selectData3(int id, Integer page, Integer size, SelectQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select * from tb_info_detail where fk_basic_info_id=? and 1=1 ");
        params.add(id);
        if(!StrUtil.isNullOrEmpty( query.getInfoDetailName() )){
            sqlBuilder.append(" and  info_detail_name like concat('%',?,'%') ");
            params.add( query.getInfoDetailName()  );
        }
        if( query.getInfoDetailDesc()!=null ){
            sqlBuilder.append(" and  info_detail_desc like concat('%',?,'%') ");
            params.add( query.getInfoDetailDesc() );
        }
        if( query.getFkBasicInfoId()!=null ){
            sqlBuilder.append(" and  fk_basic_info_id like concat('%',?,'%') ");
            params.add( query.getFkBasicInfoId() );
        }
        sqlBuilder.append(" limit ?,?");
        params.add(  (page-1)*size  );
        params.add( size );
        return DBUtil.executeDQL(sqlBuilder.toString(), DBUtil.getConn(),new BeanListHandler<>(InfoDetail.class,true), params.toArray() );

    }

    @Override
    public Integer selectTotal3(int id,SelectQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select count(*) from tb_info_detail where fk_basic_info_id=? and 1=1 ");
        params.add(id);
        if(!StrUtil.isNullOrEmpty( query.getInfoDetailName() )){
            sqlBuilder.append(" and  info_detail_name like concat('%',?,'%') ");
            params.add( query.getInfoDetailName()  );
        }
        if( query.getInfoDetailDesc()!=null ){
            sqlBuilder.append(" and  info_detail_desc like concat('%',?,'%') ");
            params.add( query.getInfoDetailDesc() );
        }
        if( query.getFkBasicInfoId()!=null ){
            sqlBuilder.append(" and  fk_basic_info_id like concat('%',?,'%') ");
            params.add( query.getFkBasicInfoId() );
        }
        Long aLong = DBUtil.executeDQL(sqlBuilder.toString(), new NumberHandler<>(Long.class), params.toArray());
        return aLong.intValue();
    }


//    public int findBasicPrice2(int g_id) {
//        String sql = "select good_cost from tb_good where good_id=?";
//        Long aLong = DBUtil.executeDQL(sql, DBUtil.getConn(), new NumberHandler<>(Long.class), g_id);
//        return aLong.intValue();
//    }

//    @Override
//    public int getTotal(int g_id, String[] ids) {
//        String sql = "select sum(good_discount) from tb_good_detail where fk_good_id=2 and fk_info_detail_id in (";
//        for (int i = 0; i < ids.length; i++) {
//            if( i!= ids.length-1){
//                sql += " ?, ";
//            }else{
//                sql += "?)";
//            }
//        }
//        Long a = DBUtil.executeDQL(sql,DBUtil.getConn(),new NumberHandler<>(Long.class),ids);
//        return a.intValue();
//    }
}



